package org.apogee.mil.client.main.pages.trainee.forms;

import java.util.ArrayList;
import java.util.List;

import org.apogee.mil.client.utils.KeyCodes;
import org.apogee.mil.client.widgets.DialogHelper;
import org.apogee.mil.shared.Question;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;

public class QuestionForm extends LayoutContainer {
	
	private List<Question> questions;
	private List<Question> correct;
	private List<Question> wrong;
	private FormData data;
	private FormPanel panel;
	
	private TextArea question;
	private TextArea choice1;
	private TextArea choice2;
	private TextArea choice3;
	private TextArea choice4;
	
	private TextField<String> answerField;
	
	private int currentQuestion = 0;
	
	
	public QuestionForm(List<Question> questions) {
		this.questions = questions;
		
		this.correct = new ArrayList<Question>();
		this.wrong = new ArrayList<Question>();
	}
	
	
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		
		data = new FormData("100%");
		
		panel = new FormPanel();
		panel.setFrame(true);
		panel.setBorders(true);
		panel.setSize(600, 600);
		panel.setBodyBorder(true);
		panel.setLabelAlign(LabelAlign.RIGHT);
		panel.setLabelWidth(100);
		panel.setButtonAlign(HorizontalAlignment.CENTER);
		
		
		question = new TextArea();
		question.setSize(700, 100);
		question.setFieldLabel("Question");
		question.setReadOnly(true);
		
		panel.add(question, data);
		
		choice1 = new TextArea();
		choice1.setSize(700, 50);
		choice1.setFieldLabel("1-A");
		choice1.setReadOnly(true);
		
		panel.add(choice1, data);
		
		choice2 = new TextArea();
		choice2.setSize(700, 50);
		choice2.setFieldLabel("2-B");
		choice2.setReadOnly(true);
		
		panel.add(choice2, data);
		
		choice3 = new TextArea();
		choice3.setSize(700, 50);
		choice3.setFieldLabel("3-C");
		choice3.setReadOnly(true);
		
		panel.add(choice3, data);
		
		choice4 = new TextArea();
		choice4.setSize(700, 50);
		choice4.setFieldLabel("4-D");
		choice4.setReadOnly(true);
		
		panel.add(choice4, data);
		
		answerField = new TextField<String>();
		answerField.setWidth(100);
		answerField.setFieldLabel("Answer");
		answerField.addKeyListener(new KeyListener(){
			
			public void componentKeyDown(ComponentEvent event) {
				if (event.getKeyCode() == KeyCodes.ENTER) {
					checkAnswer();
				} else if (event.getKeyCode() == KeyCodes.F2) {
					currentQuestion--;
					getCurrentQuestion();
				} else if (event.getKeyCode() == KeyCodes.F3) {
					currentQuestion++;
					getCurrentQuestion();
				}
			}
		});
		
		panel.add(answerField);
		
		Button submit = new Button("Submit");
		submit.setToolTip("Submit your answer");
		submit.setWidth(100);
		submit.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				checkAnswer();
				getCurrentQuestion();
				
			}
		});
		
		String nbsp = "";
		
		for (int i = 0; i < 65; i++)
			nbsp += "&nbsp;";
		
		
		panel.add(new Label(nbsp));
		panel.add(new Label(nbsp));
		panel.add(new Label(nbsp));
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.setHorizontalAlign(HorizontalAlignment.CENTER);
		
		
		VerticalPanel vp = new VerticalPanel();
		vp.setVerticalAlign(VerticalAlignment.MIDDLE);
		vp.setHorizontalAlign(HorizontalAlignment.CENTER);
		
		
		
		hp.add(new Label(nbsp));

		
		hp.add(submit);
		vp.add(hp);
		
		hp = new HorizontalPanel();
		hp.setHorizontalAlign(HorizontalAlignment.CENTER);
		
		Button next = new Button(">>");
		next.setToolTip("Next Quesiton");
		next.setWidth(50);
		next.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				wrong.add(questions.get(currentQuestion));
				currentQuestion++;
				if (currentQuestion == questions.size()) {
					finish();
				} else {
					getCurrentQuestion();
				}
				
			}

		});
		
		Button prev = new Button("<<");
		prev.setToolTip("Previous Question");
		prev.setWidth(50);
		prev.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				currentQuestion--;
				if (wrong.contains(questions.get(currentQuestion))) {
					wrong.remove(questions.get(currentQuestion));
				} else if (correct.contains(questions.get(currentQuestion))) {
					correct.remove(questions.get(currentQuestion));
				}
				currentQuestion = currentQuestion < 0 ? 0 : currentQuestion;
				
				getCurrentQuestion();
			}
		});
		
		hp.add(new Label(nbsp));
		hp.add(prev);
		hp.add(next);
		
		vp.add(hp);
		
		panel.add(vp, data);
	    
	    FormButtonBinding binding = new FormButtonBinding(panel);  
	    binding.addButton(submit);
	    binding.addButton(prev);
	    binding.addButton(next);
		
		
		add(panel);
		layout();
		
		Begin();
	}


	private void Begin() {
		
		getCurrentQuestion();
		
	}


	private void getCurrentQuestion() {
		System.out.println("Current Question: " + currentQuestion);
		if (currentQuestion == questions.size()) {
			finish();
			return;
		}
		
		panel.setHeading("Question " + (currentQuestion+1) + " out of " + questions.size());
		
		question.setValue(questions.get(currentQuestion).getQuestion());
		
		choice1.setValue(questions.get(currentQuestion).getChoice(0));
		choice2.setValue(questions.get(currentQuestion).getChoice(1));
		choice3.setValue(questions.get(currentQuestion).getChoice(2));
		choice4.setValue(questions.get(currentQuestion).getChoice(3));
		
		answerField.focus();
		
		panel.layout();
		panel.repaint();
		
	}
	
	private void finish() {
		System.out.println("Finished");
		DialogHelper helper = new DialogHelper("Finished", new FinishedForm(wrong, correct.size(), questions.size()));
		helper.show();
		getParent().setVisible(false);
	}
	
	private void checkAnswer() {
		if (currentQuestion == questions.size()) {
			finish();
			return;
		}
		int correctAnswer = questions.get(currentQuestion).getAnswer();
		
		String val = answerField.getValue().toLowerCase().trim() == null ? "0" : answerField.getValue().toLowerCase().trim();
		
		if (val.equals("")) {
			val = "0";
		}
		
		int answer = 0;
		
		try {
			answer = Integer.parseInt(val); 
		} catch (Exception ex) {
			if (val.equals("a")) {
				answer = 1;
			} else if (val.equals("b")) {
				answer = 2;
			} else if (val.equals("c")) {
				answer = 3;
			} else if (val.equals("d")) {
				answer = 4;
			} else {
				answer = 0;
			}
		}
		
		if (answer < 1 || answer > 4) {
			MessageBox.alert("Error", "Answer must be either between 1-4 or A-D", null);
			return;
		}
		
		questions.get(currentQuestion).setSelectedAnswer(answer);
		
		if (answer != correctAnswer) {
			Info.display("Info", "xxxxxxxx Wrong Answer xxxxxxxxx");
			wrong.add(questions.get(currentQuestion));
		} else {
			Info.display("Info", "-----------> Correct Answer <---------------");
			correct.add(questions.get(currentQuestion));
		}
		
		currentQuestion++;
		answerField.setValue("");
		answerField.focus();
		
	}
}
